﻿using System;
using System.Web.UI.WebControls;
using Business;
using EntitiesTM;

namespace TimeWork
{
    public partial class ListDepartment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["email"] == null)
            {
                Response.Redirect("http://"+Request.Url.Authority+"/Login.aspx");
            }
            if (!IsPostBack)
            {
                FillGrid();
            }
        }

        protected void GVListDepartment_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GVListDepartment.EditIndex = e.NewEditIndex;
            FillGrid();
        }

        protected void GVListDepartment_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GVListDepartment.EditIndex = -1;
            FillGrid();
        }

        public void FillGrid()
        {
            GVListDepartment.DataSource = BuDepartment.selAllDepartment(Session["companyid"].ToString());
            GVListDepartment.DataBind();
        }

        protected void GVListDepartment_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            EnDepartment department = new EnDepartment();
            department.departmentName = ((TextBox)GVListDepartment.Rows[e.RowIndex].FindControl("txtDepartmentName")).Text;
            department.departmentNameAlt = ((TextBox)GVListDepartment.Rows[e.RowIndex].FindControl("txtDepartmentNameAlt")).Text;
            department.departmentDescription = ((TextBox)GVListDepartment.Rows[e.RowIndex].FindControl("txtDepartmentDescription")).Text;
            department.departmentID = Int32.Parse(GVListDepartment.DataKeys[e.RowIndex].Values[0].ToString());
            BuDepartment.updDepartment(department, Session["companyid"].ToString());
            GVListDepartment.EditIndex = -1;
            FillGrid();
        }

        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            lblName.Text = String.Empty;
            lblErrorNameAlt.Text = String.Empty;

            string name = txtName.Text.Trim();
            string nameAlt = txtNameAlt.Text.Trim();
            if (name.Equals("") && nameAlt.Equals(""))
            {
                lblName.Text = "không được để trống";
                lblErrorNameAlt.Text = "không được để trống";
            }
            else
            {
                EnDepartment department = new EnDepartment();
                department.departmentName = txtName.Text.Trim();
                department.departmentNameAlt = txtNameAlt.Text.Trim();
                department.departmentDescription = txtDescription.Text.Trim();
                ClearControl();
                BuDepartment.insDepartment(department, Session["companyid"].ToString());
                FillGrid();
            }
        }

        public void ClearControl()
        {
            txtName.Text = String.Empty;
            txtNameAlt.Text = String.Empty;
            txtDescription.Text = String.Empty;
        }

        protected void GVListDepartment_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            EnDepartment department = new EnDepartment();
            department.departmentID = Int32.Parse(GVListDepartment.DataKeys[e.RowIndex].Values[0].ToString());
            BuDepartment.delDepartment(department, Session["companyid"].ToString());
            FillGrid();
        }


    }
}